package com.hqyj.springBoot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.springBoot.modules.account.dao.UserRoleDao;
import com.hqyj.springBoot.modules.account.entity.UserRole;
import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.modules.common.vo.Search;
import com.hqyj.springBoot.modules.account.dao.UserDao;
import com.hqyj.springBoot.modules.account.entity.User;
import com.hqyj.springBoot.modules.account.service.UserService;
import com.hqyj.springBoot.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public Result<User> login(User user) {
        User temp = userDao.getUserByUserNameAndPassword(
                user.getUserName(), MD5Util.getMD5(user.getPassword()));
        if (temp != null) {
            return new Result<User>(Result.ResultStatus.SUCCESS.code, "登录成功");
        } else {
            return new Result<User>(Result.ResultStatus.FAILED.code, "用户名或密码错误");
        }
    }

    @Override
    @Transactional
    public Result<User> insertUser(User user) {
        User temp = userDao.selectOneByUserName(user.getUserName());

        //判断是否有重复user
        if (temp != null){
            return new Result<>(Result.ResultStatus.FAILED.code, "User is repeat");
        }
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        user.setCreateDate(new Date());

        userDao.insertUser(user);

        if (user.getRoles() != null){
            user.getRoles().stream()
                    .forEach(item -> {
                        userRoleDao.insertUserRole(new UserRole(user.getUserId(),
                                item.getRoleId()));
                    });
        }



        return new Result<>(Result.ResultStatus.SUCCESS.code, "Insert User success",user);
    }

    @Override
    @Transactional
    public Result<User> updateUser(User user) {

        User temp = userDao.selectOneByUserName(user.getUserName());

        if (temp != null && temp.getUserId() == user.getUserId()){
            return new Result<>(Result.ResultStatus.FAILED.code, "UserName is repeat");
        }

        userDao.updateUser(user);

        return new Result<>(Result.ResultStatus.SUCCESS.code, "Update success",user);

    }

    @Override
    @Transactional
    public Result<Object> deleteUserById(int userId) {
        userDao.deleteUserById(userId);
        return new Result<>(Result.ResultStatus.SUCCESS.code, "Delete success");
    }

    @Override
    @Transactional
    public User selectUserById(int userId) {
        return userDao.selectUserById(userId);
    }

    @Override
    @Transactional
    public PageInfo<User> getUserBySearch(Search search) {
        //初始化search
        search.init();
        //开启分页
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());


        return PageInfo.of(Optional.ofNullable(userDao.getUserBySearch(search))
                                                .orElse(Collections.emptyList()));
    }
}
